//
//  MMGVisitorView.swift
//  MyMicroblog
//
//  Created by Mac on 17/1/5.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SnapKit

// 提供代理协议
@objc protocol MMGVisitorViewDelegate:NSObjectProtocol {

    // 声明协议方法,分别在注册和登录按钮点击的时候执行
    // 两个方法应该是可选的
    @objc optional func userWillLogin()
    @objc optional func userWillRegister()
}

class MMGVisitorView: UIView {
    
    // 提供代理属性
    weak var delegate: MMGVisitorViewDelegate?
    
    // 提供修改大图片和提示信息的方法,判断是否是 home 页面
    func updataInfo(imageName:String?, tipTitle:String?, isHome:Bool) {
        if isHome {
        
            self.startAnimation()
        }else{
        
            self.circleImage.image = UIImage(named: imageName!)
            self.tipLabel.text = tipTitle
            self.houseImage.isHidden = true
            self.coverView.isHidden = true
        }
    }
    
    // 添加动画方法
    private func startAnimation() {
        
        if MMGUserAccountViewModel.shared.userLogin {
        
            return
        }
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.isRemovedOnCompletion = false
        
        self.circleImage.layer.add(animation, forKey: nil)
    }
    
    // 提供构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 背景色 232,233,232
        
        backgroundColor = UIColor(red: 235 / 255.0, green: 236 / 255.0, blue: 235 / 255.0, alpha: 1)
        
        addSubview(circleImage)
        addSubview(coverView)
        addSubview(houseImage)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        loginButton.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
        registerButton.addTarget(self, action: #selector(clickRegisterButton), for: .touchUpInside)
        
        circleImage.snp.makeConstraints { (make) in
//            make.center.equalTo(self)
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-100)
        }
        
        houseImage.snp.makeConstraints { (make) in
            make.center.equalTo(circleImage)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(circleImage)
            make.top.equalTo(circleImage.snp.bottom).offset(20)
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.left.equalTo(tipLabel)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.top.height.width.equalTo(loginButton)
            make.right.equalTo(tipLabel)
        }
        
        coverView.snp.makeConstraints { (make) in
            make.width.left.bottom.equalTo(circleImage)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 在懒加载房子图片
    private lazy var houseImage: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    // 在懒加载中,可以省略闭包中的参数部分
    private lazy var circleImage: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    // 懒加载提示 label
    private lazy var tipLabel: UILabel = {
        
        let v = UILabel(text: "关注一些人,回到这里看看有什么惊喜", textColor: UIColor.darkGray, textFontSize: 14)
        
        v.textAlignment = .center
        v.preferredMaxLayoutWidth = 220
        v.numberOfLines = 0
        
        return v
    }()
    
    // 懒加载登录 button
    lazy var  loginButton: UIButton = {
        
        let v = UIButton(titleNormal: "登录", titleHighlighted: "登录", titleColorNormal: UIColor.lightGray, titleColorHighLighted: UIColor.orange, titleFontSize: 17, frame: CGRect.zero, backgroundImageNormal: #imageLiteral(resourceName: "common_button_white"), backgroundImageHight: #imageLiteral(resourceName: "common_button_white"))
        v.sizeToFit()
        
        return v
    }()
    
    // 懒加载注册 button
    lazy var  registerButton: UIButton = {
        
        let v = UIButton(titleNormal: "注册", titleHighlighted: "注册", titleColorNormal: UIColor.orange, titleColorHighLighted: UIColor.orange, titleFontSize: 17, frame: CGRect.zero, backgroundImageNormal: #imageLiteral(resourceName: "common_button_white"), backgroundImageHight: #imageLiteral(resourceName: "common_button_white"))
        v.sizeToFit()
        
        return v
    }()
    
    
    // 懒加载遮罩
    // 懒加载
    private lazy var coverView: UIImageView = {
        
        let v = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
        v.sizeToFit()
        
        return v
    }()
    
    // 登录点击事件
    @objc private func clickLoginButton() {
    
        self.delegate?.userWillLogin?()
    }
    
    @objc private func clickRegisterButton() {
    
        self.delegate?.userWillRegister?()
    }

}
